package com.xxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxy.entity.Category;
import com.xxy.entity.dto.CategoryQueryDTO;
import com.xxy.mapper.CategoryMapper;
import com.xxy.result.PageResult;
import com.xxy.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void update(Category category) {
        this.updateById(category);
    }

    @Override
    public PageResult query(CategoryQueryDTO categoryQueryDTO) {
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        //name模糊查询
        if (categoryQueryDTO.getName()!=null && !categoryQueryDTO.getName().isEmpty()) {
            queryWrapper.like(Category::getName,categoryQueryDTO.getName());
        }
        //isUse精确查询
        if (categoryQueryDTO.getIsUse()!=null) {
            queryWrapper.eq(Category::getIsUse,categoryQueryDTO.getIsUse());
        }
        //根据id降序
        queryWrapper.orderByDesc(Category::getId);
        //分页
        Integer current = categoryQueryDTO.getCurrent();

        Integer size = categoryQueryDTO.getSize();
        Page<Category> page = new Page<>(current, size);
        Page<Category> resultPage=categoryMapper.selectPage(page,queryWrapper);

        List<Category> records = resultPage.getRecords();
        long total = resultPage.getTotal();

        return new PageResult(total,records);
    }
}
